#include "whalestreamtab.h"
#include "whaleabstractentry.h"
#include "whalebutton.h"
#include "../logic/whaleabstractconnect.h"

#include <QVBoxLayout>
#include <QtDebug>
#include <QScrollArea>
#include <QLabel>
#include <QtDebug>

WhaleStreamTab::WhaleStreamTab( QWidget * parent ) : WhaleTabBase( parent ) {
    // define the text later with setTitleText()
    this->mTitle = new QLabel( "" );
    this->mTitle->setObjectName( "tabTitleLabel" );

    // set close button
    WhaleButton * closeButton = new WhaleButton();
    closeButton->setText( "X" );
    closeButton->setObjectName( "tabCloseButton" );
    closeButton->setFixedWidth( 40 );

    // horizontal layout for title and close button
    QHBoxLayout * topLayout = new QHBoxLayout();
    topLayout->addWidget( this->mTitle );
    topLayout->addWidget( closeButton );

    // create a widget holding the top layout
    this->mTabTop = new QWidget();
    this->mTabTop->setLayout( topLayout );
    this->mTabTop->setObjectName( "tabTop" );

    // set the stream vertical layout
    this->mStreamLayout = new QVBoxLayout();

    // set the main widget holding all stream entries
    QWidget * mainWidget = new QWidget();
    mainWidget->setLayout( this->mStreamLayout );

    // make the main widget scrollable
    QScrollArea * area = new QScrollArea();
    area->setWidget( mainWidget );
    area->setWidgetResizable( true );
    area->setObjectName( "streamArea" );

    // set a layout holding all tab items
    QVBoxLayout * layout = new QVBoxLayout();
    layout->addWidget( this->mTabTop );
    layout->addWidget( area );
    layout->setObjectName( "streamLayout" );

    // install the layout
    this->setLayout( layout );
    this->setObjectName( "streamTab" );

    // activate close button
    connect( closeButton, SIGNAL( clicked() ), this, SLOT( close() ) );
}

// set a custom widget for the top of the tab
void WhaleStreamTab::setTitle( QWidget * title ) {
     // delete default top
    QLayoutItem * item;
    while ( ( item = this->mTabTop->layout()->takeAt( 0 ) ) != 0 ) {
        item->widget()->deleteLater();
    }

    // delete old layout
    delete this->mTabTop->layout();


    // set a new close button
    WhaleButton * closeButton = new WhaleButton();
    closeButton->setText( "X" );
    closeButton->setObjectName( "tabCloseButton" );
    closeButton->setFixedWidth( 40 );

    // add a new layout with the close button and the given widget
    QHBoxLayout * topLayout = new QHBoxLayout();
    topLayout->addWidget( title );
    topLayout->addWidget( closeButton );

    // install the new layout
    this->mTabTop->setLayout( topLayout );

    // connect the new close button
    connect( closeButton, SIGNAL( clicked() ), this, SLOT( close() ) );
}

// set the text of the default top widget
void WhaleStreamTab::setTitleText( QString text ) {
    qobject_cast< QLabel * >( this->mTitle )->setText( text );
}

// get the top widget
QWidget * WhaleStreamTab::title() {
    return this->mTitle;
}

// attach a connector to the stream
void WhaleStreamTab::addConnector( WhaleAbstractConnect * c ) {
    // call addData on arrival of new data
    connect( c, SIGNAL( newData( QVariantList, QString ) ), this, SLOT( addData( QVariantList, QString ) ) );
}

// this is called when connector gets new data
void WhaleStreamTab::addData( QVariantList data, QString streamName ) {
    // qDebug() << "tab stream add new";
    emit this->newStreamData(); // inform other classes we have new data

    // display new data as entries
    foreach( QVariant entryData, data ) {
        if ( !entryData.canConvert( QVariant::Map ) ) { // check for data validity
            qDebug() << "invalid data sent to stream!";
            continue;
        }

        // call newEntry on child class to get a widget
        WhaleAbstractEntry * entry = this->newEntry( entryData.toMap(), streamName );

        // display returned widget
        this->mStreamLayout->addWidget( entry );

        /* TODO: start removing from the end if we have too many entries,
         * to avoid memory overflow */
    }
}

void WhaleStreamTab::clearStream() {
    QLayoutItem * item;
    while ( ( item = this->mStreamLayout->takeAt( 0 ) ) != 0 ) { // while there are items to delete
        item->widget()->deleteLater(); // delete them safely (doesn't affect loop)
    }
}

// return the main stream layout
QVBoxLayout * WhaleStreamTab::streamLayout() {
    return this->mStreamLayout;
}
